//
//  MAPYTestResultViewController.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYTestResultViewController: MAPYResultViewController {
    init(mapy_funcType: MAPYFuncType = .mapy_speed, mapy_resultModel: Any? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.mapy_funcType = mapy_funcType
        self.mapy_resultModel = mapy_resultModel
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        MAPYManager.mapy_manager.mapy_delegate = nil
        MAPYManager.mapy_manager.mapy_stopSpeedScan()
        
        mapy_navView.mapy_titleLabel.text = mapy_funcType.mapy_title
        mapy_funcModels = mapy_funcModels.filter({ mapy_model in
            return mapy_model.mapy_type != mapy_funcType
        })
    }
    // MARK: - mapy action
    override func mapy_collectionViews() {
        super.mapy_collectionViews()
        view.addSubview(mapy_navView)
        mapy_collectionView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalTo(mapy_navView.snp.bottom)
        }
        mapy_collectionView.register(MAPYIPResultInfoCell.classForCoder(), forCellWithReuseIdentifier: MAPYIPResultInfoCell.mapy_viewToString)
        mapy_collectionView.register(MAPYSpeedResultInfoCell.classForCoder(), forCellWithReuseIdentifier: MAPYSpeedResultInfoCell.mapy_viewToString)
        mapy_collectionView.register(MAPYTestResultItemCell.classForCoder(), forCellWithReuseIdentifier: MAPYTestResultItemCell.mapy_viewToString)
        mapy_collectionView.dataSource = self
        mapy_collectionView.delegate = self
    }
    // MARK: - mapy lazy
    var mapy_funcType: MAPYFuncType = .mapy_speed
    var mapy_resultModel: Any?
    override func mapy_baseBgName() -> String? {
        return "mapy_result_testBg"
    }
    lazy var mapy_navView: MAPYNavView = {
        let mapy_navView = MAPYNavView()
        mapy_navView.mapy_leftBlock = {[weak self] in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return mapy_navView
    }()
    lazy var mapy_funcModels: [MAPYFuncModel] = {
        var mapy_funcModels = [MAPYFuncModel]()
        mapy_funcModels.append(MAPYFuncModel(mapy_type: .mapy_connect,mapy_title: "Better node location", mapy_icon: "mapy_result_testBetter"))
        mapy_funcModels.append(MAPYFuncModel(mapy_type: .mapy_speed,mapy_title: "Speed test", mapy_icon: "mapy_result_testSpeed2"))
        mapy_funcModels.append(MAPYFuncModel(mapy_type: .mapy_ip,mapy_title: "IP test", mapy_icon: "mapy_result_testIp2"))
        return mapy_funcModels
    }()
}

extension MAPYTestResultViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 1 {
            return mapy_funcModels.count
        }
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 1 {
            let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYTestResultItemCell.mapy_viewToString, for: indexPath) as! MAPYTestResultItemCell
            mapy_cell.mapy_funcModel = mapy_funcModels[indexPath.row]
            return mapy_cell
        }
        if mapy_funcType == .mapy_ip {
            let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYIPResultInfoCell.mapy_viewToString, for: indexPath) as! MAPYIPResultInfoCell
            mapy_cell.mapy_ipScanModel = (mapy_resultModel as? MAPYIPScanModel)
            return mapy_cell
        }
        let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYSpeedResultInfoCell.mapy_viewToString, for: indexPath) as! MAPYSpeedResultInfoCell
        mapy_cell.mapy_speedValueModel = (mapy_resultModel as? MAPYSpeedValueModel)
        return mapy_cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: mapy_SCREEN_WIDTH-40, height: 86)
        }
        return CGSize(width: mapy_SCREEN_WIDTH-40, height: 172)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 25, right: 0)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            return
        }
        let mapy_funcModel = mapy_funcModels[indexPath.row]
        if mapy_funcModel.mapy_type == .mapy_connect {
            MAPYManager.mapy_manager.mapy_connectSmartAction()
            navigationController?.popToRootViewController(animated: true)
            return
        }
        navigationController?.pushViewController(MAPYScanViewController(mapy_funcType: mapy_funcModel.mapy_type), animated: true)
    }
}
